let fs = require("fs");
let path = require("path");

class CreateProject {
    constructor(rootPath, projectName) {
        this.rootPath = rootPath; //根目录
        this.projectName = projectName; //项目名称
        this.subFiles = ["images", "css", "js", "index.html"];
        this.iFiles = ["index.css", "index.js"];
    }
    initProject() {
        //1. 创建站点文件夹
        let projectPath = path.join(this.rootPath, this.projectName);
        // console.log(projectPath);
        fs.mkdirSync(projectPath);
        //2.创建子文件和子文件夹
        this.subFiles.forEach((fileName) => {
            // console.log(fileName);
            //判断名称是否为空, 如果是空, 那么则创建目录, 否则就创建文件
            if (path.extname(fileName) === "") {
                let dirPath = path.join(projectPath, fileName);
                fs.mkdirSync(dirPath);
            } else {
                let filePath = path.join(projectPath, fileName);
                fs.writeFileSync(filePath, "html:5");
            }
        })
        //3. 创建后缀文件

        let jsPath = path.join(this.rootPath, this.projectName, "js");
        let cssPath = path.join(this.rootPath, this.projectName, "css");
        // console.log(jsPath);
        // console.log(cssPath);

        this.iFiles.forEach((iFiles) => {
            // console.log(iFiles);
            //判断是后缀 , 如果后缀相等, 则创建响应文件
            if (path.extname(iFiles) === ".css") {
                let filePath = path.join(cssPath, iFiles);
                fs.writeFileSync(filePath, "");
            } else if (path.extname(iFiles) === ".js") {
                let filePath = path.join(jsPath, iFiles);
                fs.writeFileSync(filePath, "");
            }
        })
    }
}
//调用方法
let cp = new CreateProject(__dirname, "dynamic");
cp.initProject();